package org.juggers.client.ui;

/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

import org.juggers.client.JuggersMessages;
import org.juggers.client.controller.JuggerController;
import org.juggers.client.ui.component.ReCaptcha;
import org.juggers.domain.Jugger;
import org.juggers.validation.action.AlertAction;
import org.juggers.validation.action.ChangeStyleAction;
import org.juggers.validation.validator.EmailValidator;
import org.juggers.validation.validator.MultiValidator;
import org.juggers.validation.validator.NotEmptyValidator;
import org.juggers.validation.validator.ReCaptchaValidator;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.TextBox;
import com.google.inject.Inject;
import com.google.inject.Provider;
import com.google.inject.Singleton;

/**
 * Registration dialog box
 * 
 * @author <a href="mailto:michel@breizhjug.org">Michel DAVID</a>
 */
@Singleton
public class RegistrationDialogBox
    extends DialogBox
{

    @Inject
    private Provider<JuggerController> juggerControllerProvider;

    private ReCaptcha reCaptcha = new ReCaptcha();

    @Inject
    private RegistrationDialogBox( JuggersMessages messages )
    {
        setText( messages.registerDialogBoxText() );

        FlexTable layout = new FlexTable();
        layout.setCellSpacing( 10 );

        layout.setHTML( 0, 0, messages.registerDialogBoxEmailLabel() );
        final TextBox email = new TextBox();
        layout.setWidget( 0, 1, email );

        layout.setHTML( 1, 0, messages.registerDialogBoxFirstNameLabel() );
        final TextBox firstName = new TextBox();
        layout.setWidget( 1, 1, firstName );

        layout.setHTML( 2, 0, messages.registerDialogBoxLastNameLabel() );
        final TextBox lastName = new TextBox();
        layout.setWidget( 2, 1, lastName );

        layout.setWidget( 3, 0, reCaptcha );
        layout.getFlexCellFormatter().setColSpan( 3, 0, 2 );

        Button validate = new Button( messages.registerDialogBoxValidateButton() );
        layout.setWidget( 4, 0, validate );

        Button cancel = new Button( messages.registerDialogBoxCancelButton() );
        layout.setWidget( 4, 1, cancel );
        cancel.addClickHandler( new ClickHandler()
        {

            @Override
            public void onClick( ClickEvent arg0 )
            {
                juggerControllerProvider.get().cancelRegistration();
            }
        } );

        add( layout );

        final MultiValidator validator = new MultiValidator();
        validator.addValidator( new EmailValidator( email ).addAction( new ChangeStyleAction( email ) ) );
        validator.addValidator( new NotEmptyValidator( firstName ).addAction( new ChangeStyleAction( firstName ) ) );
        validator.addValidator( new NotEmptyValidator( lastName ).addAction( new ChangeStyleAction( lastName ) ) );
        validator.addValidator( new ReCaptchaValidator( reCaptcha ) );
        validator.addAction( new AlertAction( messages.registerDialogBoxValidationError() ) );

        validate.addClickHandler( new ClickHandler()
        {

            @Override
            public void onClick( ClickEvent arg0 )
            {
                if ( validator.validate() )
                {
                    Jugger jugger = new Jugger();
                    jugger.setEMail( email.getText() );
                    jugger.setFirstName( firstName.getText() );
                    jugger.setLastName( lastName.getText() );
                    juggerControllerProvider.get().register( jugger, reCaptcha.getChallenge(), reCaptcha.getResponse() );
                }
            }
        } );
    }

    /**
     * Reload captcha
     */
    public void reloadCaptcha()
    {
        reCaptcha.reload();
    }
}
